from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from home import  models,homeser
# Create your views here.

from django.core.cache import cache
#redis的全局配置


class HeaderNav(APIView):
    def get(self,request):
        #设置缓存的key
        key='headernav'
        #去缓存中读取数据
        navlist=cache.get(key)
        if navlist:
            return Response({'code': '200','msg':'ok','data':navlist})

        headernav = models.Nav.objects.filter(flag=True, position=0).order_by('order')
        ser = homeser.NavSerializer(instance=headernav, many=True)

        #将数据添加到缓存中
        cache.set(key,ser.data,60*60*24)


        return Response({'msg':'ok','data':ser.data})



class FootNav(APIView):
    def get(self,request):
        key='footnav'
        navlist=cache.get(key)
        if  navlist:
            return Response({"code":200,"msg":"ok","data":navlist})

        footnav=models.Nav.objects.filter(flag=True,position=1).order_by('order','id')
        print(footnav)
        ser=homeser.NavSerializer(instance=footnav,many=True)
        #将数据添加到缓存中
        cache.set(key,ser.data,60*60*24)

        return Response({'msg':'ok','data':ser.data})

from rest_framework.generics import ListAPIView
class Banner(ListAPIView):
    queryset = models.Banner.objects.filter(flag=1).order_by('order')
    serializer_class = homeser.BannerSerializer



